<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-button icon="el-icon-edit" size="mini" @click="handleSend"
          >发布公告</el-button
        >

        <el-table
          v-loading="loading"
          row-key="id"
          :data="announcementList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="公告ID" align="center" />
          <el-table-column prop="title" label="公告标题" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="nickName" label="发布者" align="center" />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
  </div>
</template>

<script>
// import { getToken } from '@/utils/auth'
import { listAnnouncement, delAnnouncement } from "@/api/info/announcement";

export default {
  name: "announcementList",
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: undefined,
      },
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      announcementList: [],
      showSearch: true,
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    //点击发布公告按钮
    handleSend() {
      this.$router.push("/writeannouncement");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listAnnouncement(this.queryParams).then((response) => {
        this.announcementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除分类编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAnnouncement(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleUpdate(row) {
      //id传递正确
      this.$router.push({
        path: "/updateAnnouncement",
        query: { id: row.id },
      });
    },
  },
};
</script>
